package com.c.service.impl;

import com.alibaba.fastjson.JSON;
import com.c.pojo.entity.Account;
import com.c.pojo.entity.Answer;
import com.c.pojo.vo.AnswerVo;
import com.c.repository.AnswerRepository;
import com.c.service.AnswerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author bysun
 */
@Service
public class AnswerServiceImpl implements AnswerService {

    final static Logger logger = LoggerFactory.getLogger(AnswerServiceImpl.class);

    @Resource
    AnswerRepository answerRepository;

    @Override
    public Answer getAnswerByAccountId(Integer id) {
        return answerRepository.getAnswerByAccountId(id);
    }

    @Override
    public Answer addOrUpdateAnswer(AnswerVo answervo) {
        Answer answer = new Answer();
        Account account = new Account();
        account.setId(answervo.getAccountId());
        answer.setId(answervo.getId());
        answer.setUrl(answervo.getUrl());
        answer.setDescription(answervo.getDescription());
        answer.setPicUrl(answervo.getPicUrl());
        answer.setTitle(answervo.getTitle());
        answer.setUpdateTime(new Date());
        answer.setAccount(account);
        answer.setDelete(answervo.isDelete());
        answer.setUpdateTime(new Date());
        logger.info("answer:{}", JSON.toJSON(answer));
        return answerRepository.save(answer);
    }

    @Override
    public Answer getAnswerByAccountAccountID(String toUserName) {
        return answerRepository.getAnswerByAccountAccountId(toUserName);
    }




}
